﻿(function () {
    
    function Common() {
        var self = this;

    }

    //取URL参数
    //使用范例：document.URL.getQueryString("p");
    String.prototype.getQueryString = function (name) {
        var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)"), r;
        if (r = this.match(reg)) return unescape(r[2]); return null;
    };
    
    //请求数据
    Common.prototype.ajax = function (cbSuccess, cbError, url, data, type, async) {
        if (async == null) async = true;
        var requestData = data;
        //get方式请求转为 key=value的形式；
        if (type != "GET") requestData = JSON.stringify(data);
        $.ajax({
            url: url,
            async: async,
            cache: false,
            data: requestData,
            type: type || "POST",
            contentType: "application/json",
            dataType: "json",
            success: cbSuccess,
            erroor: cbError
        });
    };

    //分页
     Common.prototype.Page = function (totalCount, pageSize, currentPage, callback) {
        var self = this;
        self.totalCount = ko.observable(totalCount);
        self.pageSize = ko.observable(pageSize);
        self.currentPage = ko.observable(currentPage);
        self.inputPage = ko.observable("");
        self.grayLast = ko.observable("");
        self.grayFirst = ko.observable("#999");
        self.pageCount = ko.computed(function () {
            return Math.ceil(self.totalCount() / self.pageSize());
        });
        self.gotoPage = function (data, event) {

            var indexNode = event.currentTarget;
            var index = indexNode.getAttribute("data-index");
            self.currentPage(index);
            self.styleChage(index);

            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };
        //自定义分页 add by liushu (2013-11-07)
        self.customGotoPage = function () {
            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
            self.styleChage(currentPage);
        };

        self.previousPage = function (data, event) {
            if (self.currentPage() > 1)
                self.currentPage(parseInt(self.currentPage()) - 1);

            self.styleChage(self.currentPage());

            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };
        self.nextPage = function (data, event) {
            if (self.currentPage() < self.pageCount())
                self.currentPage(parseInt(self.currentPage()) + 1);

            self.styleChage(self.currentPage());
            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };
        self.firstPage = function (data, event) {
            self.currentPage(1);

            self.styleChage(self.currentPage());
            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };
        self.lastPage = function (data, event) {
            if (self.pageCount() == 0) {
                return 0;
            }
            self.currentPage(self.pageCount());
            self.styleChage(self.currentPage());

            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };
        self.gotoInputPage = function (data, evnet) {
            var pageIndex = parseInt($("#txtInput").val());
            if (pageIndex && (pageIndex >= 1 && pageIndex <= self.pageCount())) self.currentPage(pageIndex);
            self.styleChage(self.currentPage());
            if (callback) callback(self.pageSize(), self.currentPage(), self.totalCount());
        };

        self.styleChage = function (index) {
            if (index == self.pageCount()) {
                self.grayLast("#999");
            } else {
                self.grayLast("");
            }
            if (index == 1) {
                self.grayFirst("#999");
            } else {
                self.grayFirst("");
            }
        };

    };

    //全选
    Common.prototype.selectAll = function () {

        if (event.srcElement.getAttribute("checked") == 0) {
            $(":checkbox").removeAttr("checked");
            event.srcElement.setAttribute("checked", 1);
            event.srcElement.innerText = "全选";
        } else {
            var iptObj = $(":checkbox");
            for (var i = 0; i < iptObj.length; i++) {

                if (iptObj[i].type.toLowerCase() == "checkbox" && iptObj[i].disabled == false) {
                    iptObj[i].checked = true;
                }
            }
            event.srcElement.setAttribute("checked", 0);
            event.srcElement.innerText = "全不选";
        }
    };
    common = new Common();

})();